<?php
/**
 * 后台总入口，后台除用户登录之外的所有控制器均必须继承此类
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Think\Controller;
class CommonController extends Controller{
	protected function _initialize(){
		if(!session(C('USER_AUTH_KEY'))) $this->redirect('Login/index');
		$head=D('Menu')->getHeadInfo();
		$this->assign('heading_title',isset($head['heading_title'])?$head['heading_title']:'');
		if(isset($head['breadcrumb'])) $this->assign('breadcrumb',$head['breadcrumb']);
		$user=$this->getUser();		
		$user_menu=json_decode($user['user_group']['navigation'],true);
		//是否超级管理员
		$is_administrator=(session(C('USER_AUTH_KEY'))==C('ADMINISTRATOR_USER_ID'));
		$menu_filter=array('is_show'=>1,'status'=>1);
		$menus=D('Menu')->getMenus($menu_filter);
		$r_menu=array();
		foreach ($menus as $k => $v) {
			if(in_array($v['menu_id'],$user_menu) || $is_administrator){
				$r_menu[]=array(
					'title'		=>	$v['title'],
					'pid'		=>	$v['pid'],
					'menu_id'	=>	$v['menu_id'],
					'url'		=>	$v['url'],
					'icon'		=>	$v['icon'],
					'active'	=>	(isset($head['breadcrumb'][0]['url']) && ($v['url']==$head['breadcrumb'][0]['url'] || $v['url']==$head['breadcrumb'][count($head['breadcrumb'])-1]['url'] ))?true:false
					);
			}
		}
		$r_menu=\Common\Library\Category::unlimitedForLayer($r_menu, 'children', 0,'menu_id','pid');		
		$this->assign('guser',$user);
		$this->assign('list_success',session('success'));
		$this->assign('list_danger',session('danger'));
		session('danger',null);
		session('success',null);
		$this->assign('RootMenu',$r_menu);
	}
	/**
	 * 权限判断
	 *	@param $name  - string 权限标识
	 *	@return bool  - 有权限返回true,无权限返回false
	 */
	protected function checkPermission($name){
		$return = false;
		$user_id=session(C('USER_AUTH_KEY'));
		if($user_id==C('ADMINISTRATOR_USER_ID')){
			return true;
		}
		$umap=array('user_id'=>$user_id);
		$user=M('User')->where($umap)->find();
		$pmap=array('name'=>$name);
		$permission=M('Permission')->where($pmap)->find();
		if(isset($user['user_group_id']) && isset($permission['permission_id'])){
			$map=array('user_group_id'=>$user['user_group_id']);
			$user_group=M('UserGroup')->where($map)->find();
			if($user_group){
				$user_permissions=json_decode($user_group['permission'],true);
				$return = in_array($permission['permission_id'], $user_permissions);
			}
		}
		return $return;
	}
	/**
	 * 页面错误提示，用以取代ThinkPHP的error和success的非ajax用途
	 * @param $msg - string : 提示信息
	 * @param $tmpl - string : 使用的模板
	**/
	protected function jump($msg='',$tmpl='error'){
		if($msg=='') $msg=L('error_not_permission_page');
		$this->assign('msg_content',$msg);
		$this->display('Jump/'.$tmpl);
		exit();
	}
	/**
	 * 获取当前登陆用户的信息
	 */
	protected function getUser(){
		$umap=array(
			'user_id'	=>	(int)session(C('USER_AUTH_KEY')),
			'status'	=>	1
			);
		$user=M('User')->where($umap)->find();
		if(empty($user)) $this->redirect('Login/logout');
		$gmap=array(
			'user_group_id'		=>	$user['user_group_id'],
			'status'			=>	1
			);
		$user_group=M('UserGroup')->where($gmap)->find();
		if(empty($user_group)) $this->redirect('Login/logout');
		$user['user_group']=$user_group;
		$image=image_resize($user['image'],40,40);
		$larg_image=image_resize($user['image'],100,100);
		$user['small_cover']=$image;
		$user['cover']	=	$larg_image;
		unset($user['password']);
		return $user;
	}
	/**
	 * 返回当前登录用户的user_id
	 */
	protected function getUserId(){
		return (int)session(C('USER_AUTH_KEY'));
	}
}